﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DryWCS.App.Common
{
    public class CheckHelper
    {
        #region Check
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns>null：表示Check通过</returns>
        public static string CheckNullable<T>(T t)
        {
            try
            {
                if (t == null)
                    return "请注意，没有给对应数据赋值！";
                var plist = new List<PropertyInfo>(typeof(T).GetProperties());
                foreach (var p in plist)
                {
                    if (p.GetValue(t) != null)
                    {
                        if (p.GetValue(t).ToString() != "")
                            continue;
                    }
                    object[] objAttrs = p.GetCustomAttributes(typeof(SugarColumn), true);
                    foreach (object attr in objAttrs)
                    {
                        SugarColumn column = attr as SugarColumn;
                        if (!column.IsNullable)
                            return $"{column.ColumnDescription} 不能为空！";
                    }
                }
                return null;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        #endregion
    }
}
